package com.asp.service.impl;

import com.asp.constant.MessageConstant;
import com.asp.context.BaseContext;
import com.asp.dto.AiAskDTO;
import com.asp.dto.PromptDTO;
import com.asp.entity.Dialogue;
import com.asp.entity.Prompt;
import com.asp.exception.PromptNotFoundException;
import com.asp.mapper.ConversationMapper;
import com.asp.service.ConversationService;
import com.asp.utils.AliTyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * author:          ndg
 * date:            2024/1/11
 */
@Service
public class ConversationServiceImpl implements ConversationService {


    @Autowired
    private AliTyUtils aliTyUtils;

    @Autowired
    private ConversationMapper conversationMapper;

    /*用户ai对话*/
    @Override
    public String ask(AiAskDTO aiAskDTO) {
        String answer = aliTyUtils.start(aiAskDTO.getQuestion());
        Dialogue dialogue = Dialogue.builder()
                .question(aiAskDTO.getQuestion())
                .answer(answer)
                .userName(BaseContext.getCurrentUserName())
                .build();
        conversationMapper.insert(dialogue);
        return answer;
    }

    @Override
    public List<Dialogue> dialogue() {
        String userName = BaseContext.getCurrentUserName();
        return conversationMapper.selectByUserName(userName);
    }

    @Override
    public void delete() {
        String userName = BaseContext.getCurrentUserName();
        conversationMapper.deleteByUserName(userName);
    }

    @Override
    public List<Prompt> prompt() {
        return conversationMapper.selectPrompt();
    }

    @Override
    public String promptAsk(PromptDTO promptDTO) {
        //根据提示词id 查询
        Prompt prompt = conversationMapper.selectPromptById(promptDTO.getCueWordId());
        if (prompt == null) {
            throw new PromptNotFoundException(MessageConstant.PROMPT_NOT_FOUND);
        }
        String question = prompt.getContent() + promptDTO.getQuestion();
        String answer = aliTyUtils.start(question);
        Dialogue dialogue = Dialogue.builder()
                .question(question)
                .answer(answer)
                .userName(BaseContext.getCurrentUserName())
                .build();
        conversationMapper.insert(dialogue);
        return answer;
    }
}
